use actix_web::{get, post, HttpResponse};
use actix_web::web::{Data, Json, Path, Query, ReqData};
use crate::base::{DbPool};
use crate::base::errors::AppError;
use crate::base::response::{ApiResponse, FateResult, Pagination};
use crate::data::dto::auth_dto::{AddRoleRequest, RolePageRequest};
use crate::data::model::role::Role;
use crate::service::role_service::{RoleService};

// /// 用户注册接口
// ///
// /// 用于新用户注册账号，需要提供用户名、邮箱和密码
// #[utoipa::path(
//     post,
//     path = "/api/auth/register",
//     request_body = RegisterRequest,
//     responses(
//         (status = 200, description = "注册成功", body = ApiResponse<AuthResponse>),
//         (status = 400, description = "请求数据验证失败", body = String),
//         (status = 500, description = "服务器内部错误", body = String)
//     ),
//     tag = "权限管理"
// )]
// #[post("/register")]
// pub async fn register(pool: Data<DbPool>, base: Data<Config>, data: Json<RegisterRequest>) -> Result<HttpResponse, AppError> {
//     let response = AuthService::register(&pool, base, data.into_inner()).await?;
//     Ok(ApiResponse::success(response))
// }

/// 新增角色
/// 
/// 增加新的角色信息
#[utoipa::path(
    post,
    path = "/api/role/add",
    request_body = AddRoleRequest,
    responses(
        (status = 200, description = "操作成功", body = ApiResponse<Role>),
        (status = 400, description = "请求数据验证失败", body = String),
        (status = 500, description = "服务器内部错误", body = String)
    ),
    tag = "权限管理"
)]
#[post("/add")]
pub async fn role_add(db_pool: Data<DbPool>, user_id: ReqData<i64>, params: Json<AddRoleRequest>) -> FateResult {
    let response = RoleService::add(&db_pool, &user_id, params.into_inner()).await?;
    Ok(ApiResponse::success(response))
}

/// 角色详情
/// 
/// 根据ID查询角色详情
#[utoipa::path(
    get,
    path = "/api/role/info/{id}",
    params(
        ("id" = i64, description = "角色ID")
    ),
    responses(
        (status = 200, description = "操作成功", body = ApiResponse<Role>),
        (status = 400, description = "请求数据验证失败", body = String),
        (status = 500, description = "服务器内部错误", body = String)
    ),
    tag = "权限管理"
)]
#[get("/info/{id}")]
pub async fn role_info(db_pool: Data<DbPool>,id: Path<i64>) -> FateResult {
    let response = RoleService::info(&db_pool, &id).await?;
    Ok(ApiResponse::success(response))
}

#[utoipa::path(
    get,
    path = "/api/role/page",
    params(RolePageRequest),
    responses(
        (status = 200, description = "操作成功", body = ApiResponse<Pagination<Role>>),
        (status = 400, description = "请求数据验证失败", body = String),
        (status = 500, description = "服务器内部错误", body = String)
    ),
    tag = "权限管理"
)]
#[get("/page")]
pub async fn role_page(db_pool: Data<DbPool>, params: Query<RolePageRequest>) -> FateResult {
    let response = RoleService::page(&db_pool, params.into_inner()).await?;
    Ok(ApiResponse::success(response))
}